﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using YXShop.Web;
using YXShop.Model.Order;
using System.Text.RegularExpressions;
namespace YXShop.TemplateAction
{
    public partial class LabelParse
    {
        public string ShoppingCarInfo()
        {
            string mystyle = this.Label_Inserted;
            string showMode = this.GetParamValue("YX:ShowMode");
            string TempletLayout = this.GetParamValue("YX:TempletLayout");
            StringBuilder shtml = new StringBuilder();
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();

            //添加数据
            ProfileCommon Profile = new ProfileCommon();
            ICollection<CartItemInfo> cart = Profile.ShoppingCart.CartItems;
            YXShop.BLL.Product.ProductInfo productbll = new YXShop.BLL.Product.ProductInfo();
            Common dp = new Common();
            string StrContent = "";
            if (cart.Count > 0)
            {
                if (!string.IsNullOrEmpty(mystyle))
                {
                    string pattern = @"\{\#[\s\S]*?\#\}";
                    string tempfinallyconent = mystyle;
                    StringBuilder GetTemp = new StringBuilder();
                    MatchCollection macths = Regex.Matches(tempfinallyconent, pattern);
                    string Str_Tag = "";
                    string temptag = "";
                    foreach (Match macth in macths)
                    {
                        temptag = macth.ToString();
                        int templength = temptag.ToString().IndexOf("{#") + 2;
                        int templength2 = temptag.ToString().IndexOf("#}");
                        if (temptag.ToString().Substring(templength, templength2 - templength).ToString() != null)
                        {
                            Str_Tag = temptag.ToString().Substring(templength, templength2 - templength);
                        }
                    }
                    StringBuilder cartInfo = new StringBuilder();
                    double productTotalPrice = 0;
                    double productTotalWeight = 0;
                    double productTotalIntegral = 0;
                    int Pro_TotalProductCount = 0;
                    foreach (CartItemInfo info in cart)
                    {
                        YXShop.Model.Product.ProductInfo productmodel = productbll.GetModel(int.Parse(info.ProductId));
                        if (productmodel != null)
                        {
                            if (Str_Tag != "")
                            {
                                StrContent = Str_Tag;
                            }
                            else
                            {
                                StrContent = mystyle;
                            }
                            double price = 0.0;
                            price = double.Parse(productmodel.pro_ShopPrice.ToString());
                            if (HttpContext.Current.Session["MemberID"] != null)
                            {
                                price = YXShop.Common.ProductInfo.DiscountedPrice(Convert.ToInt32(HttpContext.Current.Session["MemberID"].ToString()), price, productmodel.pro_RatingDiscount);
                            }
                            if (StrContent.IndexOf("[#Pro_Images]") > -1)
                            {
                                StrContent = StrContent.Replace("[#Pro_Images]", sp.DummyPaht + productmodel.pro_Thumbnail);
                            }
                            if (StrContent.IndexOf("[#Pro_Name]") > -1)
                            {
                                string strUrl = Common.UrlStr("productcontent", "q_productid=" + info.ProductId + "", "product/");
                                StrContent = StrContent.Replace("[#Pro_Name]", "<a href=" + strUrl + " target='_blank'>" + productmodel.pro_Name + "</a>");
                            }

                            if (StrContent.IndexOf("[#Pro_Price]") > -1)
                            {
                                StrContent = StrContent.Replace("[#Pro_Price]", productmodel.pro_MarketPrice.ToString());
                            }
                            if (StrContent.IndexOf("[#Pro_ShopPrice]") > -1)
                            {
                                StrContent = StrContent.Replace("[#Pro_ShopPrice]", price.ToString());
                            }
                            if (StrContent.IndexOf("[#Pro_Integral]") > -1)
                            {
                                StrContent = StrContent.Replace("[#Pro_Integral]", productmodel.pro_DonateIntegral.ToString());
                            }

                            cartInfo.Append(StrContent);
                        }


                    }
                    if (Str_Tag != "")
                    {
                        if (cartInfo.ToString() != string.Empty)
                        {
                            mystyle = mystyle.Replace(temptag, cartInfo.ToString());
                        }
                        else
                        {
                            mystyle = mystyle.Replace(temptag, "");
                        }
                        shtml.Append(mystyle);
                    }
                    else
                    {
                        shtml.Append(cartInfo.ToString());
                    }
                }
                else
                {
                    return "";
                }
            }
            else
            {
                return "";
            }


            return shtml.ToString();
        }
    }
}